package cn.tedu._03vehicle.base.exception;

import cn.tedu._03vehicle.base.response.jsonresulte;
import cn.tedu._03vehicle.base.response.statuscode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@RestControllerAdvice
@Slf4j

public class globleexception
{
    //父类异常处理
@ExceptionHandler(RuntimeException.class)
    public jsonresulte handleRuntimeException(RuntimeException e)
    {
        String data=e.getMessage();
        return jsonresulte.failed(data);
    }

    @ExceptionHandler(Throwable.class)
    public jsonresulte handleThrowable(Throwable e)
    {
        String data=e.getMessage();
        log.error("服务器异常",e);
        return new jsonresulte(statuscode.THROWABLE_ERROR,data);
    }
    //封装参数异常处理
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public jsonresulte handleMethodArgumentNotValidException(MethodArgumentNotValidException me)
    {
        String data=me.getFieldError().getDefaultMessage();
        return new jsonresulte(statuscode.VAILDATED_ERROR,data);
    }
    //非封装参数异常处理
    @ExceptionHandler(ConstraintViolationException.class)
    public jsonresulte handleConstraintViolationException(ConstraintViolationException e)
    {
        String data=e.getMessage().split(":")[1].trim();
        return new jsonresulte(statuscode.VAILDATED_ERROR,data);
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public jsonresulte handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e)
    {
        String data="上传文件单个不得超过10mb,单次不得超过100mb";
        return new jsonresulte(statuscode.FILE_SIZE_ERROR, data);
    }
}
